package com.qlylkj.lbk.ui.user.register

import android.app.Activity
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.Observer
import com.forest.core.etx.*
import com.forest.core.widget.SmsCountDownTimer
import com.qlylkj.lbk.R
import com.qlylkj.lbk.base.BaseVMActivity
import com.qlylkj.lbk.ui.setting.AgreementActivity
import kotlinx.android.synthetic.main.activity_register.*

class RegisterActivity: BaseVMActivity<RegisterViewModel>() {

    private var passwordShow = false

    private var smsCountDownTimer: SmsCountDownTimer? = null

    override fun providerVMClass() = RegisterViewModel::class.java

    override fun setLayoutId() = R.layout.activity_register

    override fun initView() {
        phoneNumberEdit.addTextChangedListener { check() }
        verifyCodeEdit.addTextChangedListener { check() }
        passwordEdit.addTextChangedListener { check() }
        close.setOnClickListener(onClickListener)
        getVerifyCode.setOnClickListener(onClickListener)
        register.setOnClickListener(onClickListener)
        userAgreementText.setOnClickListener(onClickListener)
        passwordStatusImage.setOnClickListener(onClickListener)
        toLogin.setOnClickListener(onClickListener)
        getVerifyCode.isSelected = true
        register.isClickable = false
        userAgreementCheckBox.setOnCheckedChangeListener { _, _ ->
            check()
        }
    }

    override fun initData() {

    }

    override fun startObserve() {
        super.startObserve()
        mViewModel.apply {
            uiState.observe(this@RegisterActivity, Observer { uiModel ->
                uiModel?.run {
                    if (loginSuccess) {
                        toast("注册成功")
                        setResult(Activity.RESULT_OK)
                        finish()
                    }
                    showError?.let { err ->
                        toast(err)
                    }
                }
            })
        }
    }

    private val onClickListener = View.OnClickListener {
        when (it.id) {
            R.id.close -> finish()
            R.id.getVerifyCode -> getVerifyCode()
            R.id.passwordStatusImage -> changePasswordStatus()
            R.id.userAgreementText -> lookUserAgreement()
            R.id.register -> register()
            R.id.toLogin -> finish()
        }
    }

    private fun check() {
        val check = phoneNumberEdit.isPhoneNumber() && passwordEdit.isPassword(8) && userAgreementCheckBox.isChecked
        register.isClickable = check
        register.isSelected = check
    }

    private fun changePasswordStatus() {
        if (passwordShow) {
            passwordEdit.transformationMethod = PasswordTransformationMethod.getInstance()
            passwordStatusImage.setImageResource(R.mipmap.ic_password_display)
        } else {
            passwordEdit.transformationMethod = HideReturnsTransformationMethod.getInstance()
            passwordStatusImage.setImageResource(R.mipmap.ic_password_show)
        }
        passwordShow = !passwordShow
    }

    private fun getVerifyCode(){
        smsCountDownTimer = SmsCountDownTimer(getVerifyCode, "获取验证码")
        smsCountDownTimer?.start()
        toast("短信验证码已发送")
    }

    private fun lookUserAgreement(){
        startKtxActivity<AgreementActivity>(value = AgreementActivity.AGREEMENT_TYPE to AgreementActivity.AGREEMENT_REGISTER)
    }

    private fun register(){
        if(!userAgreementCheckBox.isChecked){
            toast("请确认已阅读并同意《用户协议和隐私条款》")
            return
        }
        mViewModel.register(phoneNumberEdit.toTrim(),passwordEdit.toTrim())
    }

    override fun onDestroy() {
        smsCountDownTimer?.cancel()
        super.onDestroy()
    }

}